﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Media.Imaging;

namespace MOMKeLABS.Controls.Popup
{
    public partial class StartMenu : UserControl
    {
        public StartMenu()
        {
            InitializeComponent();
        }

        private List<Type> Apps
        {
            get;
            set;
        }


        /// <summary>
        /// load all the types of the installed apps. That is all the controls under 
        /// the Apps name space which inhreit the IApp interface
        /// </summary>
        private void LoadApps()
        {

            this.Apps = new List<Type>();

            Assembly asm = Assembly.GetExecutingAssembly();

            foreach (Type type in asm.GetTypes())
            {
                if (type.Namespace == Constants.WindowConstants.APP_NAMESPACE)
                {
                    foreach (Type t in type.GetInterfaces())
                    {
                        if(t == typeof(Interfaces.IApp))
                            this.Apps.Add(type);
                    }
                }
            }
        }

        /// <summary>
        /// dynamically load a link to create an instance of each of the installed apps
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadApps();

            // create a link for each loaded app

            if (this.Apps == null)
                return;

            int row = 0;

            foreach (Type t in this.Apps)
            {
                // add a new row for this link
                this.Container.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

                StartMenuItem item = new StartMenuItem()
                {
                    TypeOfApp = t,
                    Caption = t.Name,
                    ImageURL = (Activator.CreateInstance(t) as Interfaces.IApp).IconURL
                };

                //HyperlinkButton lnkBtn = new HyperlinkButton()
                //{
                //    Content = t.Name,
                //    Tag = t
                //};


                //Image iconImg = new Image()
                //{
                //    Source = new BitmapImage()
                //    {
                //        UriSource = new Uri((Activator.CreateInstance(t) as Interfaces.IApp).IconURL)
                //    }
                //};

                //iconImg.SetValue(Grid.RowProperty, row);
                //iconImg.SetValue(Grid.ColumnProperty, 0);
                //iconImg.SetValue(Image.StyleProperty, App.Current.Resources["StartMenuIcons"] as Style);

                //lnkBtn.Click += new RoutedEventHandler(lnkBtn_Click);

                //lnkBtn.SetValue(Grid.ColumnProperty, 1);
                //lnkBtn.SetValue(Grid.RowProperty, row);

                //this.LayoutRoot.Children.Add(lnkBtn);
                //this.LayoutRoot.Children.Add(iconImg);

                item.Click += new StartMenuItem.ClickHandler(item_Click);

                item.SetValue(Grid.RowProperty, row);
                item.SetValue(Grid.ColumnProperty, 0);
                this.Container.Children.Add(item);
                                
                row++;
            }
        }

        void item_Click(object sender, MouseButtonEventArgs args)
        {
            if ((App.Current as App).VisualRoot.GetType() != typeof(MainPage))
            {
                return;
            }

            Type t = (sender as StartMenuItem).TypeOfApp;

            Controls.Window.Window win = new Window.Window();
            win.ContentElement = Activator.CreateInstance(t) as UserControl;
            win.HeaderCaption = (sender as StartMenuItem).Caption;
            win.HeaderIconURL = (Activator.CreateInstance(t) as Interfaces.IApp).IconURL;

            ((App.Current as App).VisualRoot as MainPage).AddWindow(win);
        }



    }
}
